package com.hunttown.mes.impl;

import com.hunttown.mes.rpc.domain.TransportExpenseDetailDTO;
import com.hunttown.mes.rpc.domain.query.TransportExpenseDetailDTOQuery;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportExpenseDetailRpcService;
import com.hunttown.mes.service.TransportExpenseDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportExpenseDetailConverts.convert;
import static com.hunttown.mes.convert.TransportExpenseDetailConverts.converto;

/**
 * created by wangjunfu on 2019-07-07 10:50:30
 */
@Service
public class TransportExpenseDetailRpcServiceImpl implements TransportExpenseDetailRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportExpenseDetailRpcServiceImpl.class);

    private final TransportExpenseDetailService transportExpenseDetailService;

    @Autowired
    public TransportExpenseDetailRpcServiceImpl(TransportExpenseDetailService transportExpenseDetailService) {
        this.transportExpenseDetailService = transportExpenseDetailService;
    }

    @Override
    public TransportExpenseDetailDTO insert(TransportExpenseDetailDTO transportExpenseDetailDTO) {
        try {
            return convert(transportExpenseDetailService.insert(convert(transportExpenseDetailDTO)));
        } catch (Exception e) {
            logger.error("TransportExpenseDetail create failed.", e);
            throw new RuntimeException("TransportExpenseDetail create failed, Please order your code.", e);
        }
    }

    @Override
    public List<TransportExpenseDetailDTO> insertForBatch(List<TransportExpenseDetailDTO> batchList) {
        try {
            return convert(transportExpenseDetailService.insertForBatch(converto(batchList)));
        } catch (Exception e) {
            logger.error("TransportExpenseDetail create failed.", e);
            throw new RuntimeException("TransportExpenseDetail insert for batch failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportExpenseDetailDTO transportExpenseDetailDTO) {
        try {
            return transportExpenseDetailService.updateInfoById(convert(transportExpenseDetailDTO));
        } catch (Exception e) {
            logger.error("TransportExpenseDetail update by id failed.", e);
            throw new RuntimeException("TransportExpenseDetail update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportExpenseDetailDTOQuery transportExpenseDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportExpenseDetailDTOQuery.getW_id() != null && transportExpenseDetailDTOQuery.getW_id() > 0) {
                query.put("w_id", transportExpenseDetailDTOQuery.getW_id());
            }

            if (transportExpenseDetailDTOQuery.getIds() != null && !transportExpenseDetailDTOQuery.getIds().equals("")) {
                query.put("ids", transportExpenseDetailDTOQuery.getIds());
            }

            return transportExpenseDetailService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportExpenseDetail update by query failed.", e);
            throw new RuntimeException("TransportExpenseDetail update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportExpenseDetailDTO selectById(Integer id) {
        try {
            return convert(transportExpenseDetailService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportExpenseDetail select by id failed.", e);
            throw new RuntimeException("TransportExpenseDetail select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportExpenseDetailDTO selectObjByQuery(TransportExpenseDetailDTOQuery transportExpenseDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(transportExpenseDetailService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportExpenseDetail select obj by query failed.", e);
            throw new RuntimeException("TransportExpenseDetail select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportExpenseDetailDTO> getListForPage(TransportExpenseDetailDTOQuery transportExpenseDetailDTOQuery) {
        Query query = Query.page(transportExpenseDetailDTOQuery.getPn(), transportExpenseDetailDTOQuery.getPageSize());

        if (transportExpenseDetailDTOQuery.getId() != null && transportExpenseDetailDTOQuery.getId() > 0) {
            query.put("id", transportExpenseDetailDTOQuery.getId());
        }

        if (transportExpenseDetailDTOQuery.getIds() != null && !transportExpenseDetailDTOQuery.getIds().equals("")) {
            query.put("ids", transportExpenseDetailDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportExpenseDetailDTOQuery.getLikeName())) {
            query.put("likeName", transportExpenseDetailDTOQuery.getLikeName());
        }

        if (transportExpenseDetailDTOQuery.getBxId() != null) {
            query.put("bxId", transportExpenseDetailDTOQuery.getBxId());
        }

        if (StringUtils.isNotBlank(transportExpenseDetailDTOQuery.getShowDate())) {
            query.put("showDate", transportExpenseDetailDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportExpenseDetailDTOQuery.getStartDate())) {
            query.put("startDate", transportExpenseDetailDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportExpenseDetailDTOQuery.getEndDate())) {
            query.put("endDate", transportExpenseDetailDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportExpenseDetailDTOQuery.getOrderBy())) {
            query.put("orderBy", transportExpenseDetailDTOQuery.getOrderBy());
        }

        List<TransportExpenseDetailDTO> list = convert(transportExpenseDetailService.selectByQuery(query));
        Page<TransportExpenseDetailDTO> page = Page.from(transportExpenseDetailDTOQuery.getPn(), transportExpenseDetailDTOQuery.getPageSize(), list);
        page.setTotalItems(transportExpenseDetailService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportExpenseDetailService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportExpenseDetail delete failed.", e);
            throw new RuntimeException("TransportExpenseDetail delete failed, Please order your code.", e);
        }
    }
}